import {
  createDic,
  findDicMany,
  updateOneDic,
  deleteOneDic,
  createPersonnel,
  findPersonnelMany,
  deletePersonnel,
  updatePersonnel,exportExcel
} from "../services/personnel";

export default {

  namespace: 'personnel',

  state: {},

  // subscriptions: {
  //   setup({ dispatch, history }) {  // eslint-disable-line
  //   },
  // },

  effects: {
    * createPersonnel({payload}, {call, put}) {
      const response = yield call(createPersonnel, payload);
      if (!response) {
        yield put({type: 'onNotify', payload: {message: "无服务！", type: 'error'}});
        return;
      }
      const {result, status, message} = response;
      if (status !== "ok") {
        yield put({type: 'onNotify', payload: {message, type: 'error'}});
        return;
      }

      yield put({type: 'onDataLoad', payload: {personnelList: []}});
      return response;
    },

    * findPersonnelMany({payload}, {call, put}) {
      const response = yield call(findPersonnelMany, payload);
      if (!response) {
        yield put({type: 'onNotify', payload: {message: "无服务！", type: 'error'}});
        return;
      }
      const {result, status, message} = response;
      if (status !== "ok") {
        yield put({type: 'onNotify', payload: {message, type: 'error'}});
        return;
      }

      yield put({type: 'onDataLoad', payload: {personnelList: result}});
      return response;
    },
    * updatePersonnel({payload}, {call, put}) {
      const response = yield call(updatePersonnel, payload);
      if (!response) {
        yield put({type: 'onNotify', payload: {message: "无服务！", type: 'error'}});
        return;
      }
      const {result, status, message} = response;
      if (status !== "ok") {
        yield put({type: 'onNotify', payload: {message, type: 'error'}});
        return;
      }
      yield put({type: 'onDataLoad', payload: {personnelList: []}});
      return response;
    },
    * deletePersonnel({payload}, {call, put}) {
      const response = yield call(deletePersonnel, payload);
      if (!response) {
        yield put({type: 'onNotify', payload: {message: "无服务！", type: 'error'}});
        return;
      }
      const {result, status, message} = response;
      if (status !== "ok") {
        yield put({type: 'onNotify', payload: {message, type: 'error'}});
        return;
      }
      yield put({type: 'onDataLoad', payload: {personnelList: []}});
      return response;
    },


    * createDic({payload}, {call, put}) {
      const response = yield call(createDic, payload);
      if (!response) {
        yield put({type: 'onNotify', payload: {message: "无服务！", type: 'error'}});
        return;
      }
      const {result, status, message} = response;
      if (status !== "ok") {
        yield put({type: 'onNotify', payload: {message, type: 'error'}});
        return;
      }

      yield put({type: 'onDataLoad', payload: {personnelData: []}});
      return response;
    },

    * findDicMany({payload}, {call, put}) {
      const response = yield call(findDicMany, payload);
      if (!response) {
        yield put({type: 'onNotify', payload: {message: "无服务！", type: 'error'}});
        return;
      }
      const {result, status, message} = response;
      if (status !== "ok") {
        yield put({type: 'onNotify', payload: {message, type: 'error'}});
        return;
      }

      yield put({type: 'onDataLoad', payload: {personnelData: result}});
      return response;
    },
    * updateDic({payload}, {call, put}) {
      const response = yield call(updateOneDic, payload);
      if (!response) {
        yield put({type: 'onNotify', payload: {message: "无服务！", type: 'error'}});
        return;
      }
      const {result, status, message} = response;
      if (status !== "ok") {
        yield put({type: 'onNotify', payload: {message, type: 'error'}});
        return;
      }
      yield put({type: 'onDataLoad', payload: {personnelData: []}});
      return response;
    },
    * deleteDic({payload}, {call, put}) {
      const response = yield call(deleteOneDic, payload);
      if (!response) {
        yield put({type: 'onNotify', payload: {message: "无服务！", type: 'error'}});
        return;
      }
      const {result, status, message} = response;
      if (status !== "ok") {
        yield put({type: 'onNotify', payload: {message, type: 'error'}});
        return;
      }
      yield put({type: 'onDataLoad', payload: {personnelData: []}});
      return response;
    },

    * exportExcel({payload}, {call, put}) {
      const response = yield call(exportExcel, payload);
      if (!response) {
        yield put({type: 'onNotify', payload: {message: "无服务！", type: 'error'}});
        return;
      }
      const {result={}, status, message} = response;
      if (status !== "ok") {
        yield put({type: 'onNotify', payload: {message, type: 'error'}});
        return;
      }

      yield put({type: 'onDataLoad', payload: {excel: result}});
      return response;
    },
  },

  reducers: {
    onDataLoad(state, {payload}) {
      state._notify = undefined;
      return {...state, ...payload};
    }
  }

};
